package gw.com.android.ui.bulletin;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.greendao.NotifyDataHelper;
import gw.com.android.utils.ThreadPool;

/**
 * @author jett
 * @since 2018-08-02.
 */
public class NotifyCategoryAdapter extends RecyclerView.Adapter<NotifyCategoryAdapter.NotifyCategoryHolder> {

    private final Activity activity;
    private final HashMap<Integer, String> categoryArr;
    private List<NotifyData> mDatas;
    private List<Long> newIds;

    NotifyCategoryAdapter(Activity activity, HashMap<Integer, String> categoryArr) {
        this.activity = activity;
        this.categoryArr = categoryArr;
    }

    public void setDatas(List<NotifyData> list) {
        this.mDatas = list;
        notifyDataSetChanged();
    }

    public void setNewIds(List<Long> newIds) {
        this.newIds = newIds;
    }

    public List<Long> getNotifyIds() {
        if (mDatas != null) {
            List<Long> ids = new ArrayList<>();
            for (NotifyData data : mDatas) {
                ids.add(data._id);
            }
            return ids;
        }
        return null;
    }

    @Override
    public NotifyCategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notify_category, parent, false);
        return new NotifyCategoryHolder(itemView);
    }

    @Override
    public void onBindViewHolder(NotifyCategoryHolder holder, int position) {
        NotifyData item = getItem(position);
        if (item == null) {
            return;
        }
        holder.mData = item;
        setDateTime(holder.tvDateTime, item, position);
        if (!item.isReaded && newIds != null && newIds.contains(item._id)) {
            holder.ivNewTag.setVisibility(View.VISIBLE);
        } else {
            holder.ivNewTag.setVisibility(View.GONE);
        }
        holder.tvTitle.setText(buildTitle(item.title, item.typeId));
        holder.tvContent.setText(item.description);
        if (item.isReaded) {
            holder.tvTitle.setTextColor(0xFFA4A7AC);
            holder.tvContent.setTextColor(0xFFA4A7AC);
            holder.tvDetail.setTextColor(0xFFA4A7AC);
        } else {
            holder.tvTitle.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_b));
            holder.tvContent.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_888888));
            holder.tvDetail.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_u));
        }
    }

    private String buildTitle(String title, int typeId) {
        if (categoryArr == null) {
            return title;
        }
        String typeName = categoryArr.get(typeId);
        if (!TextUtils.isEmpty(typeName)) {
            return "【".concat(typeName).concat("】").concat(title);
        }
        return title;
    }

    private void setDateTime(TextView tvDateTime, NotifyData data, int position) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = new Date(data.ctime * 1000);
        String dateTime = dateFormat.format(date);
        NotifyData previousItem = getItem(position - 1);
        if (previousItem != null) {
            Date previousDate = new Date(previousItem.ctime * 1000);
            String previousDateTime = dateFormat.format(previousDate);
            if (dateTime.equals(previousDateTime)) {
                tvDateTime.setVisibility(View.GONE);
                return;
            }
        }
        tvDateTime.setVisibility(View.VISIBLE);
        tvDateTime.setText(dateTime);
    }

    private NotifyData getItem(int position) {
        if (mDatas == null) {
            return null;
        }
        if (position >= 0 && position < mDatas.size()) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    class NotifyCategoryHolder extends RecyclerView.ViewHolder {

        NotifyData mData;

        View llItem;
        View rlDetail;
        TextView tvDateTime;
        ImageView ivNewTag;
        TextView tvTitle;
        TextView tvContent;
        TextView tvDetail;

        NotifyCategoryHolder(View itemView) {
            super(itemView);
            llItem = itemView.findViewById(R.id.push_item);
            rlDetail = itemView.findViewById(R.id.push_item_detail);
            tvDateTime = (TextView) itemView.findViewById(R.id.push_item_datetime);
            ivNewTag = (ImageView) itemView.findViewById(R.id.push_item_tag);
            tvTitle = (TextView) itemView.findViewById(R.id.push_item_title);
            tvContent = (TextView) itemView.findViewById(R.id.push_item_content);
            tvDetail = (TextView) itemView.findViewById(R.id.push_item_detail_tv);
            llItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setNotifyReaded(mData);
                    if (mData != null) {
                        ActivityManager.showBulletinDetailActivityOpenApi2(activity, null, null, String.valueOf(mData._id), null);
                    }
                }
            });
            rlDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setNotifyReaded(mData);
                    if (mData != null) {
                        ActivityManager.showBulletinDetailActivityOpenApi2(activity, null, null, String.valueOf(mData._id), null);
                    }
                }
            });
        }

    }

    private void setNotifyReaded(NotifyData data) {
        if (data != null) {
            //data.isReaded = true;
            //notifyDataSetChanged();
            final Long mId = data._id;
            ThreadPool.getInstance().execute(new Runnable() {
                @Override
                public void run() {
                    NotifyDataHelper.setNotifyReaded(mId);
                    NotifyDataHelper.notifyChange();
                }
            });
        }
    }

}
